home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / appleman / picprint.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-06  |  21.6 KB  |  601 lines

  1. VERSION 2.00
  2. Begin Form Picprint 
  3.    Caption         =   "Picture Print Demo"
  4.    ClientHeight    =   4020
  5.    ClientLeft      =   1380
  6.    ClientTop       =   2040
  7.    ClientWidth     =   7365
  8.    Height          =   4710
  9.    Left            =   1320
  10.    LinkMode        =   1  'Source
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   4020
  13.    ScaleWidth      =   7365
  14.    Top             =   1410
  15.    Width           =   7485
  16.    Begin CommandButton CmdPrintMF 
  17.       Caption         =   "Print Metafile"
  18.       Height          =   375
  19.       Left            =   4860
  20.       TabIndex        =   8
  21.       Top             =   2700
  22.       Width           =   2355
  23.    End
  24.    Begin CommandButton CmdPreviewMF 
  25.       Caption         =   "Preview Metafile"
  26.       Height          =   375
  27.       Left            =   4860
  28.       TabIndex        =   7
  29.       Top             =   2280
  30.       Width           =   2355
  31.    End
  32.    Begin CommandButton CmdLoadMF 
  33.       Caption         =   "Load Metafile From Disk"
  34.       Height          =   375
  35.       Left            =   4860
  36.       TabIndex        =   6
  37.       Top             =   1860
  38.       Width           =   2355
  39.    End
  40.    Begin CommandButton CmdFromBitmap 
  41.       Caption         =   "Load DIB From Bitmap"
  42.       Height          =   375
  43.       Left            =   4860
  44.       TabIndex        =   5
  45.       Top             =   180
  46.       Width           =   2355
  47.    End
  48.    Begin CommandButton CmdPreviewBitmap 
  49.       Caption         =   "Preview Bitmap"
  50.       Height          =   375
  51.       Left            =   4860
  52.       TabIndex        =   4
  53.       Top             =   1020
  54.       Width           =   2355
  55.    End
  56.    Begin PictureBox PicPreview 
  57.       Height          =   3675
  58.       Left            =   120
  59.       ScaleHeight     =   243
  60.       ScaleMode       =   3  'Pixel
  61.       ScaleWidth      =   295
  62.       TabIndex        =   3
  63.       Top             =   180
  64.       Width           =   4455
  65.    End
  66.    Begin CommandButton Command1 
  67.       Caption         =   "Load DIB From File"
  68.       Height          =   375
  69.       Left            =   4860
  70.       TabIndex        =   2
  71.       Top             =   600
  72.       Width           =   2355
  73.    End
  74.    Begin ccCallback Callback1 
  75.       IntVersion      =   5
  76.       Left            =   6660
  77.       Top             =   3300
  78.       Type            =   1  'AbortProc
  79.    End
  80.    Begin CommandButton CmdPrintBitmap 
  81.       Caption         =   "Print Bitmap"
  82.       Height          =   375
  83.       Left            =   4860
  84.       TabIndex        =   1
  85.       Top             =   1440
  86.       Width           =   2355
  87.    End
  88.    Begin PictureBox Picture1 
  89.       AutoRedraw      =   -1  'True
  90.       Height          =   735
  91.       Left            =   4680
  92.       Picture         =   PICPRINT.FRX:0000
  93.       ScaleHeight     =   705
  94.       ScaleWidth      =   1725
  95.       TabIndex        =   0
  96.       Top             =   3240
  97.       Width           =   1755
  98.    End
  99.    Begin Menu MenuConfigPrinter 
  100.       Caption         =   "ConfigPrinter"
  101.       Begin Menu MenuExtDevMode 
  102.          Caption         =   "ExtDevMode"
  103.       End
  104.       Begin Menu MenuDevMode 
  105.          Caption         =   "DevMode"
  106.       End
  107.       Begin Menu MenuPaperSizes 
  108.          Caption         =   "PaperSizes"
  109.       End
  110.    End
  111. Option Explicit
  112.     Dim dib As DIBSTRUCT
  113.     Dim hmf As Integer
  114. ' This function is called during the EndPage API function
  115. ' to allow the user to abort printing
  116. Sub Callback1_AbortProc (hPr As Integer, code As Integer, retval As Integer)
  117.     ' We must allow events to take place, otherwise the
  118.     ' user button press on the abortform form will never
  119.     ' be detected!
  120.     DoEvents
  121.     If code = SP_OUTOFDISK Or AbortPrinting% Then
  122.         retval = 0
  123.         Exit Sub
  124.     End If
  125.     retval = -1
  126. End Sub
  127. Sub CmdFromBitmap_Click ()
  128.     Screen.MousePointer = 11
  129.     LoadTheBitmap2 dib
  130.     Screen.MousePointer = 0
  131. End Sub
  132. Sub CmdLoadMF_Click ()
  133.     Dim filename$
  134.     Dim di%
  135.     filename$ = InputBox$("Enter metafile filename", "bitmap", app.Path & "\" & "answmach.wmf")
  136.     Screen.MousePointer = 11
  137.     If hmf% <> 0 Then di% = DeleteMetafile(hmf%)
  138.     hmf% = LoadTheMetafile(filename$)
  139.     Screen.MousePointer = 0
  140. End Sub
  141. Sub CmdPreviewBitmap_Click ()
  142.     If dib.bmdata = 0 Then
  143.         MsgBox "No valid bitmap"
  144.         Exit Sub
  145.     End If
  146.     picPreview.Cls
  147.     PrintBitmap picPreview.hDC, picPreview.ScaleWidth, picPreview.ScaleHeight
  148. End Sub
  149. Sub CmdPreviewMF_Click ()
  150.     If hmf% = 0 Then
  151.         MsgBox "No valid metafile"
  152.         Exit Sub
  153.     End If
  154.     picPreview.Cls
  155.     PrintTheMetafile picPreview.hDC, picPreview.ScaleWidth, picPreview.ScaleHeight
  156. End Sub
  157. ' This function prints the global DIB or metafile
  158. ' It also sets it into landscape mode - just to show
  159. ' how it is done
  160. ' mode% = 0 to print the DIB
  161. ' mode% = 1 to print the metafile
  162. Sub CmdPrint (mode%)
  163.     Dim dev$, devname$, devoutput$
  164.     Dim dm As DEVMODE, dmout As DEVMODE
  165.     Dim libhnd%
  166.     Dim bufsize%
  167.     Dim dminstring$, dmoutstring$
  168.     Dim dminaddr&, dmoutaddr&
  169.     Dim prhdc%
  170.     Dim dinfo As DOCINFO
  171.     Dim docname$
  172.     Dim oldcursor%
  173.     Dim di%
  174.     dev$ = GetDefPrinter$() ' Get default printer info
  175.     If dev$ = "" Then Exit Sub
  176.     devname$ = GetDeviceName$(dev$)
  177.     devoutput$ = GetDeviceOutput$(dev$)
  178.     ' Load the device driver library - exit if unavailable
  179.     libhnd% = LoadLibrary(GetDeviceDriver$(dev$) + ".drv")
  180.     If libhnd% = 0 Then GoTo cleanup2
  181.     ' Get a copy of the DEVMODE structure for this printer
  182.     ' First find out how big the DEVMODE structure is
  183.     bufsize% = agExtDeviceMode%(hWnd, libhnd%, 0, devname$, devoutput$, agGetAddressForObject(dm), 0, 0)
  184.     ' Allocate a buffer of that size and get a pointer to it
  185.     dminstring$ = String$(bufsize%, 0)
  186.     dminaddr& = agGetAddressForVBString&(dminstring$)
  187.     dmoutstring$ = String$(bufsize%, 0)
  188.     dmoutaddr& = agGetAddressForVBString&(dmoutstring$)
  189.     ' Get the output DEVMODE structure
  190.     di% = agExtDeviceMode(hWnd, libhnd%, dmoutaddr&, devname$, devoutput$, dminaddr&, 0, DM_OUT_BUFFER)
  191.     ' Copy the data buffer into the DEVMODE structure
  192.     agCopyDataBynum dmoutaddr&, agGetAddressForObject&(dm), 68
  193.     ' Set the orientation, and set the dmField flag so that
  194.     ' the function will know that it is valid.
  195.     dm.dmOrientation = DMORIENT_LANDSCAPE
  196.     dm.dmFields = dm.dmFields Or DM_ORIENTATION
  197.     ' We now have a DC to the default printer
  198.     ' This DC is also initialized to landscape mode
  199.     prhdc% = CreateDC%(GetDeviceDriver$(dev$) + ".drv", devname$, devoutput$, agGetAddressForObject&(dm))
  200.     If prhdc% = 0 Then GoTo cleanup2
  201.     ' The DOCINFO structure is the information that the
  202.     ' print manager will show. This also gives you the
  203.     ' opportunity of dumping output to a file.
  204.     docname$ = "Sample Document"
  205.     dinfo.cbSize = 10
  206.     dinfo.lpszDocName = agGetAddressForLPSTR&(docname$)
  207.     dinfo.lpszOutput = 0
  208.     ' The code that follows can be uncommented if you
  209.     ' have the CBK.VBX control from either the Visual
  210.     ' Basic Programmer's Guide to the Windows API - or
  211.     ' SpyWorks-VB
  212.     ' We set up the abort procdure here
  213.     'AbortPrinting% = 0
  214.     'di% = SetAbortProc(prhdc%, Callback1.ProcAddress)
  215.     ' And show the abort form which will be system modal
  216.     'AbortForm.Show
  217.     'BringWindowToTop AbortForm.hWnd
  218.     'AbortForm.Refresh
  219.     'di% = SetSysModalWindow(AbortForm.hWnd)
  220.     ' The usual print sequence here
  221.     di% = StartDoc(prhdc%, dinfo)
  222.     di% = StartPage(prhdc%)
  223.     If mode% = 0 Then
  224.         PrintBitmap prhdc%, 0, 0
  225.     Else
  226.         PrintTheMetafile prhdc%, 0, 0
  227.     End If
  228.     di% = EndPage(prhdc%)
  229.     If di% >= 0 Then di% = EndDocAPI(prhdc%)
  230.     ' You must unload it (not hide it) so that the
  231.     ' system modal state will be released.
  232.     Unload AbortForm
  233. cleanup2:
  234.     If prhdc% <> 0 Then di% = DeleteDC%(prhdc%)
  235.     If libhnd% <> 0 Then FreeLibrary libhnd%
  236. End Sub
  237. Sub CmdPrintBitmap_Click ()
  238.     If dib.bmdata = 0 Then
  239.         MsgBox "No valid bitmap"
  240.         Exit Sub
  241.     End If
  242.     Screen.MousePointer = 11
  243.     CmdPrint 0  ' Print the DIB
  244.     Screen.MousePointer = 1
  245. End Sub
  246. Sub CmdPrintMF_Click ()
  247.     If hmf% = 0 Then
  248.         MsgBox "No valid metafile"
  249.         Exit Sub
  250.     End If
  251.     Screen.MousePointer = 11
  252.     CmdPrint 1  ' Print the DIB
  253.     Screen.MousePointer = 1
  254. End Sub
  255. Sub Command1_Click ()
  256.     Dim filename$
  257.     filename$ = InputBox$("Enter bitmap filename", "bitmap", app.Path & "\" & "leaves.bmp")
  258.     Screen.MousePointer = 11
  259.     LoadTheBitmap filename$, dib
  260.     Screen.MousePointer = 0
  261. End Sub
  262. '   This function retrieves the definition of the default
  263. '   printer on this system
  264. Function GetDefPrinter$ ()
  265.     Dim def$
  266.     Dim di%
  267.     def$ = String$(128, 0)
  268.     di% = GetProfileString%("WINDOWS", "DEVICE", "", def$, 127)
  269.     def$ = agGetStringFromLPSTR$(def$)
  270.     GetDefPrinter$ = def$
  271. End Function
  272. '   This function returns the driver module name
  273. Function GetDeviceDriver$ (dev$)
  274.     Dim firstpos%, nextpos%
  275.     firstpos% = InStr(dev$, ",")
  276.     nextpos% = InStr(firstpos% + 1, dev$, ",")
  277.     GetDeviceDriver$ = Mid$(dev$, firstpos% + 1, nextpos% - firstpos% - 1)
  278. End Function
  279. '   Retrieves the name portion of a device string
  280. Function GetDeviceName$ (dev$)
  281.     Dim npos%
  282.     npos% = InStr(dev$, ",")
  283.     GetDeviceName$ = Left$(dev$, npos% - 1)
  284. End Function
  285. '   Returns the output destination for the specified device
  286. Function GetDeviceOutput$ (dev$)
  287.     Dim firstpos%, nextpos%
  288.     firstpos% = InStr(dev$, ",")
  289.     nextpos% = InStr(firstpos% + 1, dev$, ",")
  290.     GetDeviceOutput$ = Mid$(dev$, nextpos% + 1)
  291. End Function
  292. '   Loads the DIB from the specified file.
  293. Sub LoadTheBitmap (filename$, dib As DIBSTRUCT)
  294.     Dim fhnd%
  295.     Dim fileheader As BITMAPFILEHEADER
  296.     Dim di%
  297.     Dim bminfosize%
  298.     Dim bufsize&
  299.     Dim gptr&
  300.     ' Open the file to read
  301.     fhnd% = lopen(filename$, 0)
  302.     If fhnd% < 0 Then Exit Sub
  303.     ' Clear prior DIB value if present
  304.     If dib.bmdata <> 0 Then
  305.         di% = GlobalFree(dib.bmdata)
  306.         dib.bmdata = 0
  307.     End If
  308.     ' First read the file header
  309.     di% = lread(fhnd%, agGetAddressForObject(fileheader), Len(fileheader))
  310.     If (fileheader.bfType <> &H4D42) Then
  311.         di% = lclose(fhnd%)
  312.         Exit Sub
  313.     End If
  314.     ' Now we need a buffer that will contain the header and palette info
  315.     ' How large is it?
  316.     bminfosize = fileheader.bfOffBits - Len(fileheader)
  317.     dib.bminfo = String$(bminfosize, 0)
  318.     di% = lread(fhnd%, agGetAddressForVBString(dib.bminfo), bminfosize)
  319.     ' Calculate the size of the rest of the file
  320.     bufsize& = fileheader.bfSize - bminfosize - Len(fileheader)
  321.     ' Now allocate a buffer to hold the data
  322.     ' We use the global memory pool because this buffer
  323.     ' could easily be above 64k bytes.
  324.     dib.bmdata = GlobalAlloc(GMEM_MOVEABLE, bufsize&)
  325.     gptr& = GlobalLock&(dib.bmdata)
  326.     di% = hread(fhnd%, gptr&, bufsize&)
  327.     di% = GlobalUnlock(dib.bmdata)
  328.     di% = lclose(fhnd%)
  329. End Sub
  330. ' This function loads a DIB from the bitmap in picture1
  331. Sub LoadTheBitmap2 (dib As DIBSTRUCT)
  332.     Dim bi As BITMAPINFO
  333.     Dim dctemp%, dctemp2%
  334.     Dim msg$
  335.     Dim bufsize&
  336.     Dim bm As BITMAP
  337.     Dim ghnd%
  338.     Dim gptr&
  339.     Dim xpix%, ypix%
  340.     Dim doscale%
  341.     Dim uy%, ux%
  342.     Dim di%
  343.     ' Clear prior DIB value if present
  344.     If dib.bmdata <> 0 Then
  345.         di% = GlobalFree(dib.bmdata)
  346.         dib.bmdata = 0
  347.     End If
  348.     ' Create a temporary memory DC and select into it
  349.     ' the background picture of the picture1 control.
  350.     dctemp% = CreateCompatibleDC(picture1.hDC)
  351.     ' Get the size of the picture bitmap
  352.     di% = GetObjectAPI%(picture1.Picture, 14, agGetAddressForObject(bm))
  353.     ' Fill the BITMAPINFO for the desired DIB
  354.     bi.bmiHeader.biSize = 40
  355.     bi.bmiHeader.biWidth = bm.bmWidth
  356.     bi.bmiHeader.biHeight = bm.bmHeight
  357.     bi.bmiHeader.biPlanes = 1
  358.     bi.bmiHeader.biBitCount = 4
  359.     bi.bmiHeader.biCompression = BI_RGB
  360.     ' Now calculate the data buffer size needed
  361.     bufsize& = bi.bmiHeader.biWidth
  362.     ' Figure out the number of bytes based on the
  363.     ' number of pixels in each byte. In this case we
  364.     ' really don't need all this code because this example
  365.     ' always uses a 16 color DIB, but the code is shown
  366.     ' here for your future reference
  367.     Select Case bi.bmiHeader.biBitCount
  368.         Case 1
  369.             bufsize& = (bufsize& + 7) / 8
  370.         Case 4
  371.             bufsize& = (bufsize& + 1) / 2
  372.         Case 24
  373.             bufsize& = bufsize& * 3
  374.     End Select
  375.     ' And make sure it aligns on a long boundary
  376.     bufsize& = ((bufsize& + 3) / 4) * 4
  377.     ' And multiply by the # of scan lines
  378.     bufsize& = bufsize& * bi.bmiHeader.biHeight
  379.     ' Now allocate a buffer to hold the data
  380.     ' We use the global memory pool because this buffer
  381.     ' could easily be above 64k bytes.
  382.     ghnd% = GlobalAlloc(GMEM_MOVEABLE, bufsize&)
  383.     gptr& = GlobalLock&(ghnd%)
  384.     di% = GetDIBitsBynum%(dctemp%, picture1.Picture, 0, bm.bmHeight, gptr&, bi, DIB_RGB_COLORS)
  385.     di% = GlobalUnlock(ghnd%)   ' Matching unlock
  386.     ' Now store the info in the DIB structure
  387.     dib.bmdata = ghnd%
  388.     dib.bminfo = String$(Len(bi), 0)
  389.     agCopyDataBynum agGetAddressForObject(bi), agGetAddressForLPSTR(dib.bminfo), Len(bi)
  390.     di% = DeleteDC%(dctemp%)
  391. End Sub
  392. Function LoadTheMetafile% (filename$)
  393.     Dim fhnd%
  394.     Dim mfile As METAFILEHEADER
  395.     Dim di%, dl&
  396.     Dim mfinfosize&
  397.     Dim currentfileloc&
  398.     Dim gptr&
  399.     Dim mfglbhnd%
  400.     Dim mfhnd%
  401.     ' Open the file to read
  402.     fhnd% = lopen(filename$, 0)
  403.     If fhnd% < 0 Then Exit Function
  404.     ' First read the placeable header file header
  405.     di% = lread(fhnd%, agGetAddressForObject(mfile), Len(mfile))
  406.     If mfile.key <> &H9AC6CDD7 Then
  407.         ' It's not a placeable metafile - so just seek to the start
  408.         di% = llseek(fhnd%, 0, 0)
  409.     End If
  410.     ' Now we need a buffer that will contain the metafile data
  411.     currentfileloc& = llseek(fhnd%, 0, 1)
  412.     mfinfosize& = llseek(fhnd%, 0, 2) - currentfileloc&
  413.     ' Now allocate a buffer to hold the data
  414.     ' We use the global memory pool because this buffer
  415.     ' could easily be above 64k bytes.
  416.     mfglbhnd% = GlobalAlloc(GMEM_MOVEABLE, mfinfosize&)
  417.     gptr& = GlobalLock&(mfglbhnd%)
  418.     dl& = llseek(fhnd%, currentfileloc&, 0)
  419.     dl& = hread(fhnd%, gptr&, mfinfosize)
  420.     di% = GlobalUnlock(mfhnd%)
  421.     di% = lclose(fhnd%)
  422.     mfhnd% = SetMetaFileBitsBetter(mfglbhnd%)
  423.     ' Don't delete the global handle - it holds the metafile data
  424.     LoadTheMetafile = mfhnd%
  425. End Function
  426. Sub MenuDevMode_Click ()
  427.     Dim dev$, devname$, devoutput$
  428.     Dim libhnd%
  429.     Dim di%
  430.     dev$ = GetDefPrinter$() ' Get default printer info
  431.     If dev$ = "" Then Exit Sub
  432.     devname$ = GetDeviceName$(dev$)
  433.     devoutput$ = GetDeviceOutput$(dev$)
  434.     ' Load the device driver library - exit if unavailable
  435.     libhnd% = LoadLibrary(GetDeviceDriver$(dev$) + ".drv")
  436.     If libhnd% = 0 Then Exit Sub
  437.     ' WARNING - this allows change of the default printer
  438.     ' settings!
  439.     di% = agDeviceMode(hWnd, libhnd%, devname$, devoutput$)
  440.     FreeLibrary (libhnd%)
  441. End Sub
  442. '   Demonstration of the Extended Device Mode function
  443. Sub MenuExtDevMode_Click ()
  444.     Dim dev$, devname$, devoutput$
  445.     Dim dm As DEVMODE, dmout As DEVMODE
  446.     Dim libhnd%
  447.     Dim bufsize%
  448.     Dim dminstring$, dmoutstring$
  449.     Dim dminaddr&, dmoutaddr&
  450.     Dim di%
  451.     dev$ = GetDefPrinter$() ' Get default printer info
  452.     If dev$ = "" Then Exit Sub
  453.     devname$ = GetDeviceName$(dev$)
  454.     devoutput$ = GetDeviceOutput$(dev$)
  455.     ' Load the device driver library - exit if unavailable
  456.     libhnd% = LoadLibrary(GetDeviceDriver$(dev$) + ".drv")
  457.     If libhnd% = 0 Then Exit Sub
  458.     bufsize% = agExtDeviceMode%(hWnd, libhnd%, 0, devname$, devoutput$, agGetAddressForObject(dm), 0, 0)
  459.     dminstring$ = String$(bufsize%, 0)
  460.     dmoutstring$ = String$(bufsize%, 0)
  461.     dminaddr& = agGetAddressForVBString&(dminstring$)
  462.     dmoutaddr& = agGetAddressForVBString&(dmoutstring$)
  463.     ' The output DEVMODE structure will reflect any changes
  464.     ' made by the printer setup dialog box.
  465.     ' Note that no changes will be made to the default
  466.     ' printer settings!
  467.     di% = agExtDeviceMode(hWnd, libhnd%, dmoutaddr&, devname$, devoutput$, dminaddr&, 0, DM_IN_BUFFER Or DM_IN_PROMPT Or DM_OUT_BUFFER)
  468.     ' Copy the data buffer into the DEVMODE structure
  469.     agCopyDataBynum dmoutaddr&, agGetAddressForObject&(dmout), 68
  470.     ShowDevMode dmout
  471. cleanup:
  472.     FreeLibrary (libhnd%)
  473. End Sub
  474. '   This function shows how to use the agDeviceCapabilities
  475. '   function to find out how many paper names the device
  476. '   supports. This technique can be used for any
  477. '   device capability
  478. Sub MenuPaperSizes_Click ()
  479.     Dim dev$, devname$, devoutput$
  480.     Dim libhnd%
  481.     Dim papercount%
  482.     Dim papername$
  483.     Dim a$, crlf$, tname$
  484.     Dim x%
  485.     Dim di%
  486.     crlf$ = Chr$(13) + Chr$(10)
  487.     dev$ = GetDefPrinter$() ' Get default printer info
  488.     If dev$ = "" Then Exit Sub
  489.     devname$ = GetDeviceName$(dev$)
  490.     devoutput$ = GetDeviceOutput$(dev$)
  491.     ' Load the device driver library - exit if unavailable
  492.     libhnd% = LoadLibrary(GetDeviceDriver$(dev$) + ".drv")
  493.     If libhnd% = 0 Then Exit Sub
  494.     ' Find out how many paper names there are
  495.     papercount% = agDeviceCapabilities(libhnd%, devname$, devoutput$, DC_PAPERNAMES, 0, 0)
  496.     If papercount% = 0 Then
  497.         MsgBox "No paper names available", 0, "Paper name capability"
  498.         Exit Sub
  499.     End If
  500.     ' Now dimension the string large enough to hold them all
  501.     papername$ = String$(64 * papercount%, 0)
  502.     di% = agDeviceCapabilities(libhnd%, devname$, devoutput$, DC_PAPERNAMES, agGetAddressForVBString&(papername$), 0)
  503.     ' Now display the results
  504.     For x% = 1 To papercount%
  505.         tname$ = Mid$(papername$, (x% - 1) * 64 + 1)
  506.         a$ = a$ + agGetStringFromLPSTR$(tname$) + crlf$
  507.     Next x%
  508.     MsgBox a$, 0, "Paper Names for Default Printer"
  509.     FreeLibrary (libhnd%)
  510. End Sub
  511. ' Print the DIB to fit the size specified (in pixels)
  512. ' If x%,y% are both 0, uses the device extents
  513. Sub PrintBitmap (ByVal hDC%, ByVal x%, ByVal y%)
  514.     Dim bih As BITMAPINFOHEADER
  515.     Dim xfer$
  516.     Dim bufsize&
  517.     Dim ghnd%
  518.     Dim gptr&
  519.     Dim xpix%, ypix%
  520.     Dim doscale%
  521.     Dim uy%, ux%
  522.     Dim di%
  523.     Dim scalefactorX As Single, scalefactorY As Single
  524.     Dim scalefactor
  525.     If dib.bmdata = 0 Then
  526.         MsgBox "No valid bitmap"
  527.         Exit Sub
  528.     End If
  529.     ' Copy the information into a structure so we can access it
  530.     agCopyDataBynum agGetAddressForVBString(dib.bminfo), agGetAddressForObject(bih), Len(bih)
  531.     gptr& = GlobalLock(dib.bmdata)
  532.     If x% = 0 And y% = 0 Then
  533.         ' We want to scale it to fill the page
  534.         xpix% = GetDeviceCaps(hDC%, HORZRES)
  535.         ' We subtract off the size of the bitmap already
  536.         ' printed, plus some extra space
  537.         ypix% = GetDeviceCaps(hDC%, VERTRES)
  538.     Else
  539.         xpix% = x%
  540.         ypix% = y%
  541.     End If
  542.     ' We need to figure out how to scale it for the page
  543.     scalefactorX = CSng(xpix%) / CSng(bih.biWidth)
  544.     scalefactorY = CSng(ypix%) / CSng(bih.biHeight)
  545.     ' Use the smaller scalefactor
  546.     If scalefactorX < scalefactorY Then scalefactor = scalefactorX Else scalefactor = scalefactorY
  547.     ' Find out the largest multiplier we can use and still
  548.     ' fit on the page
  549.     ux% = CInt(bih.biWidth * scalefactor)
  550.     uy% = CInt(bih.biHeight * scalefactor)
  551.     di% = StretchDIBitsBynum(hDC%, 0, 0, ux%, uy%, 0, 0, bih.biWidth, bih.biHeight, gptr&, agGetAddressForVBString(dib.bminfo), DIB_RGB_COLORS, SRCCOPY)
  552.     ' Dump the global memory block
  553.     di% = GlobalUnlock(ghnd%)
  554. End Sub
  555. Sub PrintTheMetafile (ByVal hDC%, ByVal x%, ByVal y%)
  556.     Dim xpix%, ypix%
  557.     Dim uy%, ux%
  558.     Dim di%
  559.     Dim dl&
  560.     Dim savedidx%
  561.     If hmf% = 0 Then
  562.         MsgBox "No valid metafile"
  563.         Exit Sub
  564.     End If
  565.     If x% = 0 And y% = 0 Then
  566.         ' We want to scale it to fill the page
  567.         xpix% = GetDeviceCaps(hDC%, HORZRES)
  568.         ' We subtract off the size of the bitmap already
  569.         ' printed, plus some extra space
  570.         ypix% = GetDeviceCaps(hDC%, VERTRES)
  571.     Else
  572.         xpix% = x%
  573.         ypix% = y%
  574.     End If
  575.     savedidx% = SaveDC(hDC%)
  576.     ' We need to set the extents
  577.     ' ANISOTROPIC mode fills the entire area
  578.     di% = SetMapMode(hDC%, MM_ANISOTROPIC)
  579.     dl& = SetWindowExt(hDC%, xpix%, ypix%)
  580.     dl& = SetViewportExt(hDC%, xpix%, ypix%)
  581.     di% = PlayMetaFile(hDC%, hmf%)
  582.     di% = RestoreDC(hDC%, savedidx%)
  583. End Sub
  584. ' Shows information about the current device mode
  585. Sub ShowDevMode (dm As DEVMODE)
  586.     Dim crlf$
  587.     Dim a$
  588.     crlf$ = Chr$(13) + Chr$(10)
  589.     a$ = "Device name = " + agGetStringFromLPSTR$(dm.dmDeviceName) + crlf$
  590.     a$ = a$ + "Devmode Version: " + Hex$(dm.dmSpecVersion) + ", Driver version: " + Hex$(dm.dmDriverVersion) + crlf$
  591.     a$ = a$ + "Orientation: "
  592.     If dm.dmOrientation = DMORIENT_PORTRAIT Then a$ = a$ + "Portrait" Else a$ = a$ + "Landscape"
  593.     a$ = a$ + crlf$
  594.     a$ = a$ + "Field mask = " + Hex$(dm.dmFields) + crlf$
  595.     a$ = a$ + "Copies = " + Str$(dm.dmCopies) + crlf$
  596.     If dm.dmFields And DM_YRESOLUTION <> 0 Then
  597.         a$ = a$ + "X,Y resolution = " + Str$(dm.dmPrintQuality) + "," + Str$(dm.dmYResolution) + crlf$
  598.     End If
  599.     MsgBox a$, 0, "Devmode structure"
  600. End Sub
  601.